package cn.com.czpb.client.module.personal.acticity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import cn.com.czpb.client.R;
import cn.com.czpb.client.app.MToolBarActivity;
import cn.com.czpb.client.custorm.smartrefreshalayout.ClassHeader_other;
import cn.com.czpb.client.model.usercenter.ComBean;
import cn.com.czpb.client.model.usercenter.ComEntity;
import cn.com.czpb.client.module.personal.adapter.MyCommAdapter;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.ParamUtils;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class MyCommActivity extends MToolBarActivity implements OnRefreshListener, OnLoadmoreListener {

    private MyCommAdapter adapter;
    @BindView(R.id.swipe_layout)
    SmartRefreshLayout swipe_layout;
    @BindView(R.id.ptrlistview_activity_collection)
    RecyclerView recyclerview_comm;
    @BindView(R.id.mycom_title)
    RelativeLayout mycom_title;
    List<ComBean> list;
    @BindView(R.id.ll_back)
    LinearLayout ll_back;

    private int Page = 1;
    private static final int PageSize = 15;
    private boolean mFlagRefresh = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        getCommForServer();
    }

    @Override
    protected int setLayoutId() {

            return R.layout.activity_comm;
    }

    private void init() {
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });


        mycom_title.setVisibility(View.VISIBLE);
        LinearLayoutManager manager = new LinearLayoutManager(activity);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        list = new ArrayList<>();
        adapter = new MyCommAdapter(list);
        recyclerview_comm.setLayoutManager(manager);
        recyclerview_comm.setAdapter(adapter);
        swipe_layout.setRefreshHeader(new ClassHeader_other(this));
        swipe_layout.setOnRefreshListener(this);
        swipe_layout.setOnLoadmoreListener(this);
    }

    private void getCommForServer() {
        Map<String, String> map = new HashMap<>();
        map.put("uid", spu.getUser().getUid());
        map.put("siteid", "1");
        map.put("Page", Page + "");
        map.put("PageSize", PageSize + "");
        Factory.provideHttpService().my_comm(ParamUtils.getRequestParam(map))
                .onBackpressureBuffer()
                .subscribeOn(Schedulers.io())
                .filter(new Func1<ComEntity, Boolean>() {
                    @Override
                    public Boolean call(ComEntity comentity) {
                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ComEntity>() {
                    @Override
                    public void call(ComEntity comentity) {
                        if (mFlagRefresh) {
                            swipe_layout.finishRefresh();
                            swipe_layout.setLoadmoreFinished(true);//恢复上拉状态
                        }
                        LogUtils.e("code--" + comentity.code);
//                        ptrlistview_activity_comm.onRefreshComplete();
                        if ("200".equals(comentity.code)) {
                            LogUtils.e("code--" + comentity.msg);
                            addDataList(comentity.data);
                        } else if ("209".equals(comentity.code)) {
                            list.clear();
                            adapter.notifyDataSetChanged();
                        }
                    }

                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.i("throwable--" + throwable.getMessage());
                    }
                });
    }

    private void addDataList(List<ComBean> mlist) {
        if (mFlagRefresh) {
            adapter.setNewData(mlist);
            adapter.notifyDataSetChanged();
        } else {
            adapter.addData(mlist);
            adapter.notifyDataSetChanged();
            adapter.loadMoreComplete();
        }
        LogUtils.i("size-->" + adapter.getData().size());
        if (mlist.size() < PageSize) {
            //关闭刷新
            adapter.loadMoreEnd(true);
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        refreshLayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                Page = 1;
                mFlagRefresh = true;
                getCommForServer();
                swipe_layout.finishRefresh();
                swipe_layout.setLoadmoreFinished(false);
            }
        },2000);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshLayout) {

    }
}

